﻿// MathExpression.cs
//
// Author: Luoyingliang
// Email: 731984870@qq.com
// 
// Copyright (c) 2018 Team Acoris
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Foundation
{
    /// <summary>
    /// represent a expression tree
    /// </summary>
    public sealed class MathExpression
    {

        #region Definition

        /// <summary>
        /// 
        /// </summary>
        class node
        {
            internal node left;
            internal node right;
            internal object value;
        }

        /// <summary>
        /// 
        /// </summary>
        enum @operator
        {
            Multiple,
            Devide,
            Plus,
            Minus
        }

        /// <summary>
        /// 
        /// </summary>
        sealed class op
        {
            public @operator @operator;

            public override string ToString() {
                switch (@operator) {
                    case @operator.Devide:
                        return "/";
                    case @operator.Minus:
                        return "-";
                    case @operator.Multiple:
                        return "*";
                    case @operator.Plus:
                        return "+";
                }
                return base.ToString();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        sealed class num
        {
            public int number;

            public override string ToString() {
                return number.ToString();
            }
        }

        #endregion

        #region Expression 
        // (1 + 6 * (4 - 1) / -5 - (2 * (4 + (7 / 3)))) + 54
        static void BuildMathExpressionTree(string expression) {

        }

        void EvaluateMathExpression() {
            m_isChanged = false;

        }

        #endregion

        private node root;
        private string m_expression;
        private int m_value;
        private bool m_isChanged;

        /// <summary>
        /// gets or sets a expression
        /// </summary>
        public string Expression {
            get { return m_expression; }
            set {
                m_expression = value;
                m_isChanged = true;
                BuildMathExpressionTree(value);
            }
        }

        /// <summary>
        /// gets current expression's value
        /// </summary>
        public int Value {
            get {
                if (m_isChanged) {
                    EvaluateMathExpression();
                }
                return m_value;
            }
        }
    }
}
